﻿Imports System.Text
Imports b3t.CardGameFramework
Imports System.Windows.Media
Class MainWindow
    Dim _You As New Player
    Dim _West As New Player
    Dim _East As New Player
    Dim _dog As New Player
    Dim _deck As New Deck
    Dim numBase As Integer
    Dim Xin As Integer = 0
    Dim Xout As Integer = 0
    Dim IndexOut() As Integer = {0, 0, 0, 0, 0, 0}
    Dim IndexIn() As Integer = {0, 0, 0, 0, 0, 0}
    Dim exchange As Boolean = True
    Dim YTrickPoints As Integer
    Dim ETrickPoints As Integer
    Dim WTrickPoints As Integer
    Dim trick As New List(Of Card)
    Dim waste As New List(Of Card)
    Private _game As New TarotGame()
    Private _playerCards As Image()
    Private _dogCards As Image()
    Private _newHand As Boolean = False
    Private _newGame As Boolean
    Private expired As Boolean = False
    Private played As Boolean = False
    Private Leader As String
    Private splayer As System.Media.SoundPlayer
    Private win As Boolean = False
#Region "Game UI Methods"

    ''' <summary>
    ''' Load the Deck Card Images
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadCardSkinImages()
        'Try
        '    ' Load the card skin image from file
        '    Dim cardSkin As Image = Image.(My.Settings.CardGameImageSkinPath)

        'Catch Ex As OutOfMemoryException
        '    MsgBox("Card skin images are not loading correctly.  Make sure the card skin images are in the correct location.")
        'End Try
        _playerCards = New Image() {ppb1, ppb2, ppb3, ppb4, ppb5, ppb6, ppb7, ppb8, ppb9, ppb10, ppb11, ppb12, ppb13, ppb14, ppb15, ppb16, ppb17, ppb18, ppb19, ppb20, ppb21, ppb22, ppb23, ppb24}
        _dogCards = New Image() {dpb1, dpb2, dpb3, dpb4, dpb5, dpb6}
    End Sub

    ''' <summary>
    ''' Set up the UI for when the game is in play after the player has hit deal game
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetUpGameInPlay()
        btnDeal.IsEnabled = False
        _newHand = False
        If _You.IsDealer = True Then
            PlayerNameLabel.Content = "Click cards to exchange kitty/hand"
            Label17.Visibility = Windows.Visibility.Hidden
            Label18.Visibility = Windows.Visibility.Hidden
            btnExchange.Visibility = Visibility.Visible
        End If
    End Sub
    Private Sub PlayNextHand()
        _newGame = True
        _newHand = False
        btnDeal.IsEnabled = True
        'choose next dealer
        If _You.IsDealer Then
            _East.IsDealer = True
            _You.IsDealer = False
            t1.Foreground = Brushes.LightBlue
            t2.Foreground = Brushes.White
        ElseIf _West.IsDealer Then
            _You.IsDealer = True
            _West.IsDealer = False
            t2.Foreground = Brushes.LightBlue
            t3.Foreground = Brushes.White
        ElseIf _East.IsDealer Then
            _East.IsDealer = False
            _West.IsDealer = True
            t3.Foreground = Brushes.LightBlue
            t1.Foreground = Brushes.White
        End If
        LYTotal.Content = "0"
        LWTotal.Content = "0"
        LETotal.Content = "0"
        LWLoTrump.Content = "0"
        LELoTrump.Content = "0"
        LYLoTrump.Content = "0"
        LWHiTrump.Content = "0"
        LEHiTrump.Content = "0"
        LYHiTrump.Content = "0"
        LWTarotTrump.Content = "0"
        LETarotTrump.Content = "0"
        LYTarotTrump.Content = "0"
        LWSeq10.Content = "0"
        LESeq10.Content = "0"
        LYSeq10.Content = "0"
        LWSeq7.Content = "0"
        LESeq7.Content = "0"
        LYSeq7.Content = "0"
        LWSeq4.Content = "0"
        LESeq4.Content = "0"
        LYSeq4.Content = "0"
        LWMeld.Content = "0"
        LEMeld.Content = "0"
        LYMeld.Content = "0"
        PlayerNameLabel.Content = "Click Deal for next hand"
        Label18.Content = "West"
        Label17.Content = "East"
        'pre-load deal sound
        If My.Settings.UseSound = True Then
            splayer = New System.Media.SoundPlayer("AfterVelcro-004.wav")
            splayer.Load()
        End If
    End Sub

    ''' <summary>
    ''' Set up the UI for a new game
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetUpNewGame()
        btnDeal.IsEnabled = True
        _You.IsDealer = True
        _West.IsDealer = False
        _East.IsDealer = False
        _newGame = True
        _newHand = False
        t1.Foreground = Brushes.White
        t2.Foreground = Brushes.LightBlue
        t3.Foreground = Brushes.White
    End Sub

    ''' Takes the card value and loads the corresponding card image from file

    Private Sub LoadCard(ByVal pb As Image, ByVal c As Card)
        Try
            Dim imagen As StringBuilder = New StringBuilder
            Select Case c.Suit
                Case Suit.Diamonds
                    imagen.Append("di_")
                Case Suit.Hearts
                    imagen.Append("he_")
                Case Suit.Spades
                    imagen.Append("sp_")
                Case Suit.Clubs
                    imagen.Append("cl_")
                Case Suit.Trumps
                    imagen.Append("tr_")
                Case Suit.Fool
                    imagen.Clear()
            End Select
            If c.Suit <> Suit.Trumps Then
                Select Case c.FaceVal
                    Case FaceValue.Ace
                        imagen.Append("1")
                    Case FaceValue.King
                        imagen.Append("14")
                    Case FaceValue.Queen
                        imagen.Append("13")
                    Case FaceValue.Knight
                        imagen.Append("12")
                    Case FaceValue.Jack
                        imagen.Append("11")
                    Case FaceValue.Ten
                        imagen.Append("10")
                    Case FaceValue.Nine
                        imagen.Append("9")
                    Case FaceValue.Eight
                        imagen.Append("8")
                    Case FaceValue.Seven
                        imagen.Append("7")
                    Case FaceValue.Six
                        imagen.Append("6")
                    Case FaceValue.Five
                        imagen.Append("5")
                    Case FaceValue.Four
                        imagen.Append("4")
                    Case FaceValue.Three
                        imagen.Append("3")
                    Case FaceValue.Two
                        imagen.Append("2")
                End Select
            Else
                Select Case c.FaceVal
                    Case FaceValue.Ace
                        imagen.Append("1")
                    Case FaceValue.Fifteen
                        imagen.Append("15")
                    Case FaceValue.Sixteen
                        imagen.Append("16")
                    Case FaceValue.Seventeen
                        imagen.Append("17")
                    Case FaceValue.Eighteen
                        imagen.Append("18")
                    Case FaceValue.Nineteen
                        imagen.Append("19")
                    Case FaceValue.Twenty
                        imagen.Append("20")
                    Case FaceValue.TwentyOne
                        imagen.Append("21")
                    Case FaceValue.Fourteen
                        imagen.Append("14")
                    Case FaceValue.Thirteen
                        imagen.Append("13")
                    Case FaceValue.Twelve
                        imagen.Append("12")
                    Case FaceValue.Eleven
                        imagen.Append("11")
                    Case FaceValue.Ten
                        imagen.Append("10")
                    Case FaceValue.Nine
                        imagen.Append("9")
                    Case FaceValue.Eight
                        imagen.Append("8")
                    Case FaceValue.Seven
                        imagen.Append("7")
                    Case FaceValue.Six
                        imagen.Append("6")
                    Case FaceValue.Five
                        imagen.Append("5")
                    Case FaceValue.Four
                        imagen.Append("4")
                    Case FaceValue.Three
                        imagen.Append("3")
                    Case FaceValue.Two
                        imagen.Append("2")
                End Select
            End If
            If c.FaceVal = FaceValue.Fool Then
                imagen.Clear()
                imagen.Append("tr_0")
            End If
            imagen.Append(".gif")
            Dim cardgameimagepath As String = ""
            If c.Suit = Suit.Trumps Then
                Select Case My.Settings.TrumpSet
                    Case Is = 0
                        cardgameimagepath = "/Images/Cards/"
                    Case Is = 1
                        cardgameimagepath = "/Images/Cards1/"
                    Case Is = 2
                        cardgameimagepath = "Images/Cards3/"
                End Select
            Else
                Select Case My.Settings.TrumpSet
                    Case Is = 0, 2
                        cardgameimagepath = "/Images/Cards/"
                    Case Is = 1
                        cardgameimagepath = "/Images/Cards1/"
                End Select
            End If
            Dim cardGameImageSkinPath As String = "/Images/Cards/back0.gif"
            imagen.Insert(0, cardgameimagepath)
            'check to see if the card should be faced down or up;
            If Not c.IsCardUp Then
                imagen.Replace(imagen.ToString, cardGameImageSkinPath)
            End If
            Dim bm As New BitmapImage
            bm.BeginInit()
            bm.UriSource = New Uri(imagen.ToString, UriKind.Relative)
            bm.DecodePixelWidth = 83
            bm.EndInit()
            pb.Source = bm
            Dim tagstr As String = c.FaceVal.ToString
            If c.Suit <> Suit.Trumps And c.FaceVal = FaceValue.Jack Then tagstr = "Jack"
            If c.Suit <> Suit.Trumps And c.FaceVal = FaceValue.Knight Then tagstr = "Knight"
            If c.Suit <> Suit.Trumps And c.FaceVal = FaceValue.Queen Then tagstr = "Queen"
            If c.Suit <> Suit.Trumps And c.FaceVal = FaceValue.King Then tagstr = "King"
            pb.Tag = tagstr & " " & c.Suit.ToString
            If c.FaceVal = FaceValue.Fool Then tagstr = "Fool"
        Catch
            MsgBox("Card images are not loading correctly.  Make sure all card images are in the right location.")
        End Try
    End Sub
    Private Function CfromTag(ByVal tstr As String) As Card
        Dim ts As String()
        Dim cresult As New Card(Suit.Clubs, FaceValue.Ace, 1, True)
        If tstr = "Fool" Then
            cresult.Suit = Suit.Fool
            cresult.FaceVal = FaceValue.Fool
            cresult.PlayValue = 0
            cresult.IsCardUp = True
            Return cresult
        End If
        ts = Split(tstr, " ", 2, )
        Select Case Trim(ts(1))
            Case Is = "Trumps"
                cresult.Suit = Suit.Trumps
            Case Is = "Clubs"
                cresult.Suit = Suit.Clubs
            Case Is = "Spades"
                cresult.Suit = Suit.Spades
            Case Is = "Hearts"
                cresult.Suit = Suit.Hearts
            Case Is = "Diamonds"
                cresult.Suit = Suit.Diamonds
        End Select
        Select Case Trim(ts(0))
            Case Is = "Fool"
                cresult.FaceVal = FaceValue.Fool
                cresult.PlayValue = 0
            Case Is = "Ace"
                cresult.FaceVal = FaceValue.Ace
                cresult.PlayValue = 1
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 10
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 100
            Case Is = "Two"
                cresult.FaceVal = FaceValue.Two
                cresult.PlayValue = 2
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 9
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 200
            Case Is = "Three"
                cresult.FaceVal = FaceValue.Three
                cresult.PlayValue = 3
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 8
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 300
            Case Is = "Four"
                cresult.FaceVal = FaceValue.Four
                cresult.PlayValue = 4
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 7
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 400
            Case Is = "Five"
                cresult.FaceVal = FaceValue.Five
                cresult.PlayValue = 5
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 6
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 500
            Case Is = "Six"
                cresult.FaceVal = FaceValue.Six
                cresult.PlayValue = 6
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 5
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 600
            Case Is = "Seven"
                cresult.FaceVal = FaceValue.Seven
                cresult.PlayValue = 7
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 4
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 700
            Case Is = "Eight"
                cresult.FaceVal = FaceValue.Eight
                cresult.PlayValue = 8
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 3
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 800
            Case Is = "Nine"
                cresult.FaceVal = FaceValue.Nine
                cresult.PlayValue = 9
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 2
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 900
            Case Is = "Ten"
                cresult.FaceVal = FaceValue.Ten
                cresult.PlayValue = 10
                If cresult.Suit = Suit.Diamonds Or cresult.Suit = Suit.Hearts Then cresult.PlayValue = 1
                If cresult.Suit = Suit.Trumps Then cresult.PlayValue = 1000
            Case Is = "Jack"
                cresult.FaceVal = FaceValue.Jack
                cresult.PlayValue = 11
            Case Is = "Knight"
                cresult.FaceVal = FaceValue.Knight
                cresult.PlayValue = 12
            Case Is = "Queen"
                cresult.FaceVal = FaceValue.Queen
                cresult.PlayValue = 13
            Case Is = "King"
                cresult.FaceVal = FaceValue.King
                cresult.PlayValue = 14
            Case Is = "Eleven"
                cresult.FaceVal = FaceValue.Eleven
                cresult.PlayValue = 1100
                'If cresult.Suit <> Suit.Trumps Then cresult.PlayValue = 11
            Case Is = "Twelve"
                cresult.FaceVal = FaceValue.Twelve
                cresult.PlayValue = 1200
                'If cresult.Suit <> Suit.Trumps Then cresult.PlayValue = 12
            Case Is = "Thirteen"
                cresult.FaceVal = FaceValue.Thirteen
                cresult.PlayValue = 1300
                'If cresult.Suit <> Suit.Trumps Then cresult.PlayValue = 13
            Case Is = "Fourteen"
                cresult.FaceVal = FaceValue.Fourteen
                cresult.PlayValue = 1400
                'If cresult.Suit <> Suit.Trumps Then cresult.PlayValue = 14
            Case Is = "Fifteen"
                cresult.FaceVal = FaceValue.Fifteen
                cresult.PlayValue = 1500
            Case Is = "Sixteen"
                cresult.FaceVal = FaceValue.Sixteen
                cresult.PlayValue = 1600
            Case Is = "Seventeen"
                cresult.FaceVal = FaceValue.Seventeen
                cresult.PlayValue = 1700
            Case Is = "Eighteen"
                cresult.FaceVal = FaceValue.Eighteen
                cresult.PlayValue = 1800
            Case Is = "Nineteen"
                cresult.FaceVal = FaceValue.Nineteen
                cresult.PlayValue = 1900
            Case Is = "Twenty"
                cresult.FaceVal = FaceValue.Twenty
                cresult.PlayValue = 2000
            Case Is = "TwentyOne"
                cresult.FaceVal = FaceValue.TwentyOne
                cresult.PlayValue = 2100
        End Select
        cresult.IsCardUp = True
        Return cresult
    End Function
#End Region

    Public Sub DealNewGame()
        Dim _deck As New Deck
        _deck.Shuffle()
        'Reset the player hands in case this is not the first game
        _You.NewHand()
        _West.NewHand()
        _East.NewHand()
        _dog.NewHand()
        YTrickPoints = 0
        ETrickPoints = 0
        WTrickPoints = 0
        'Deal cards to each person's hand
        Dim i As Integer = 0
        While i < 24
            Dim c As Card = _deck.Draw
            _You.Hand.Cards.Add(c)
            Dim d As Card = _deck.Draw
            _East.Hand.Cards.Add(d)
            Dim e As Card = _deck.Draw
            _West.Hand.Cards.Add(e)
            i = i + 1
        End While
        'sort three hands
        SortHand(_You.Hand)
        SortHand(_East.Hand)
        SortHand(_West.Hand)
        _newHand = True
        For j As Integer = 0 To 5
            Dim f As Card = _deck.Draw
            _dog.Hand.Cards.Add(f)
        Next
        'Give the players a handle to the current deck
        _You.CurrentDeck = _deck
        _West.CurrentDeck = _deck
        _East.CurrentDeck = _deck
        'play deal sound
        If My.Settings.UseSound = True Then
            splayer.PlaySync()
        End If
    End Sub
    Private Sub ClearTable()
        Dim ic As Image
        For i As Integer = 0 To 23
            _playerCards(i).Source = Nothing
            _playerCards(i).Visibility = Visibility.Hidden
            'reset your card positions
            ic = Me.FindName("ppb" + CStr(24 - i))
            ic.SetValue(LeftProperty, CDbl((7 + i) * 35))
        Next
        For i As Integer = 0 To 5
            _dogCards(i).Source = Nothing
            _dogCards(i).Visibility = Visibility.Hidden
        Next
        ppbw.Source = Nothing
        ppbw.Visibility = Visibility.Hidden
        ppbe.Source = Nothing
        ppbe.Visibility = Visibility.Hidden
        ppby.Source = Nothing
        ppby.Visibility = Visibility.Hidden
        waste.Clear()
        LblScoring.Visibility = Visibility.Hidden
    End Sub
    Public Sub SortHand(ByVal aHand As TarotHand)
        Dim cval(23) As Integer
        Dim temp As Card
        Dim tempi As Integer
        For i As Integer = 0 To 23
            cval(i) = aHand.Cards(i).Suit + aHand.Cards(i).PlayValue
        Next
        For j = 1 To 24
            For i As Integer = 1 To 23
                If cval(i - 1) < cval(i) Then
                    temp = aHand.Cards(i - 1)
                    tempi = cval(i - 1)
                    aHand.Cards(i - 1) = aHand.Cards(i)
                    cval(i - 1) = cval(i)
                    aHand.Cards(i) = temp
                    cval(i) = tempi
                End If
            Next
        Next
    End Sub
    Public Sub New()
        InitializeComponent()
        LoadCardSkinImages()
        SetUpNewGame()
    End Sub

    Private Sub btnEndGame_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEndGame.Click
        Me.Close()
    End Sub

    Private Sub wndPlay_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        LEScore.Content = "0"
        LYScore.Content = "0"
        LWScore.Content = "0"
        LETotal.Content = "0"
        LYTotal.Content = "0"
        LWTotal.Content = "0"
        LEMeld.Content = "0"
        LYMeld.Content = "0"
        LWMeld.Content = "0"
        LESeq4.Content = "0"
        LYSeq4.Content = "0"
        LWSeq4.Content = "0"
        LESeq7.Content = "0"
        LYSeq7.Content = "0"
        LWSeq7.Content = "0"
        LESeq10.Content = "0"
        LYSeq10.Content = "0"
        LWSeq10.Content = "0"
        LETarotTrump.Content = "0"
        LYTarotTrump.Content = "0"
        LWTarotTrump.Content = "0"
        LEHiTrump.Content = "0"
        LYHiTrump.Content = "0"
        LWHiTrump.Content = "0"
        LELoTrump.Content = "0"
        LYLoTrump.Content = "0"
        LWLoTrump.Content = "0"
        For i As Integer = 1 To 24
            ImageVis("ppb", i, False)
        Next
        For i As Integer = 1 To 6
            ImageVis("dpb", i, False)
        Next
        _You.IsDealer = True
        btnContinue.Visibility = Visibility.Hidden
        btnExchange.Visibility = Visibility.Hidden
        LblScoring.Visibility = Visibility.Hidden
        PWest.Visibility = Visibility.Hidden
        PEast.Visibility = Visibility.Hidden
        PYou.Visibility = Visibility.Hidden
        ImageWin.Visibility = Visibility.Hidden
        TextBlockWin.Visibility = Visibility.Hidden
        ClearTable()
        'check for the first run -- then show rules & options
        If My.Settings.FirstRun = True Then
            My.Settings.FirstRun = False
            My.Windows.OptionsWindow.Show()
        End If
        'pre-load deal sound
        If My.Settings.UseSound = True Then
            splayer = New System.Media.SoundPlayer("AfterVelcro-004.wav")
            splayer.Load()
        End If
    End Sub
    Private Sub ImageVis(ByVal base As String, ByVal t As Integer, ByVal state As Boolean)
        Dim ic As Image = Me.FindName(base + CStr(t))
        If state = True Then
            ic.Visibility = System.Windows.Visibility.Visible
        Else
            ic.Visibility = System.Windows.Visibility.Hidden
        End If
    End Sub

    Private Sub btnDeal_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDeal.Click
        'Clear the table, set up the UI for playing a game, and deal a new game
        If TextBlockWin.Visibility = Visibility.Visible Then
            TextBlockWin.Visibility = Visibility.Hidden
            ImageWin.Visibility = Visibility.Hidden
        End If
        ClearTable()
        SetUpGameInPlay()
        DealNewGame()
        If LblScoring.Visibility = Visibility.Visible Then LblScoring.Visibility = Visibility.Hidden
        'show your hand
        For i As Integer = 0 To 5
            If _You.IsDealer = True Then
                _dog.Hand.Cards(i).IsCardUp = True
            Else
                _dog.Hand.Cards(i).IsCardUp = False
            End If
            LoadCard(_dogCards(i), _dog.Hand.Cards(i))
            _dogCards(i).Opacity = 1
            _dogCards(i).ToolTip = _dog.Hand.Cards(i).ToString
        Next
        For i As Integer = 0 To 23
            LoadCard(_playerCards(i), _You.Hand.Cards(i))
            _playerCards(i).Visibility = True
            _playerCards(i).ToolTip = _You.Hand.Cards(i).ToString
            _playerCards(i).Opacity = 1
        Next
        'animation
        Dim s As Animation.Storyboard
        s = CType(Me.FindResource("DealYou"), Animation.Storyboard)
        AddHandler s.Completed, AddressOf s_Completed
        For i As Integer = 1 To 24
            ImageVis("ppb", i, True)
        Next
        Me.BeginStoryboard(s)
        If _You.IsDealer = True Then
            Leader = "East"
            exchange = True
            Xin = 0
            Xout = 0
            played = False
            btnExchange.Content = "Exchange(0)"
            btnExchange.Visibility = Visibility.Visible
            waste.Clear()
        Else
            btnExchange.Visibility = Visibility.Hidden
            If _West.IsDealer = True Then
                ChooseFromKitty("West")
                Leader = "You"
                waste.Clear()
                exchange = False
                ShowMeld()
                PlayLoop()
            End If
            If _East.IsDealer = True Then
                ChooseFromKitty("East")
                Leader = "West"
                waste.Clear()
                exchange = False
                ShowMeld()
                PlayLoop()
            End If
        End If
    End Sub

    Private Sub s_Completed(ByVal sender As Object, ByVal e As EventArgs)
        If _You.IsDealer = True Then
            'use this to deal dog cards
            Dim d As Animation.Storyboard
            d = CType(Me.FindResource("DealDog"), Animation.Storyboard)
            For i As Integer = 1 To 6
                ImageVis("dpb", i, True)
            Next
            Me.BeginStoryboard(d)
        End If
    End Sub
    Private Sub dpb1_Click(ByVal sender As Image, ByVal e As System.EventArgs) Handles dpb1.MouseDown, dpb2.MouseDown, dpb3.MouseDown, dpb4.MouseDown, dpb5.MouseDown, dpb6.MouseDown
        Dim MoveUp As New Animation.DoubleAnimation
        MoveUp.To = 190
        MoveUp.Duration = TimeSpan.FromSeconds(0.1)
        If sender.Opacity = 1.0 And Xin < 6 Then
            sender.Opacity = 0.98
            sender.BeginAnimation(TopProperty, MoveUp)
            Xin += 1
            IndexIn(Xin - 1) = CInt(Strings.Right(sender.Name, 1))
        Else
            sender.Opacity = 1.0
            MoveUp.To = 210
            sender.BeginAnimation(TopProperty, MoveUp)
            Xin -= 1
            If Xin > 0 Then IndexIn(Xin - 1) = CInt(Strings.Right(sender.Name, 1))
        End If
        If Xin = Xout Then
            btnExchange.Content = "Exchange(" + CStr(Xin) + ")"
            btnExchange.Visibility = Visibility.Visible
        Else
            btnExchange.Visibility = Visibility.Hidden
        End If
    End Sub
   

    Private Sub ppb1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ppb1.MouseDown, ppb2.MouseDown, ppb3.MouseDown, ppb4.MouseDown, ppb5.MouseDown, ppb6.MouseDown, ppb7.MouseDown, ppb8.MouseDown, ppb9.MouseDown, ppb10.MouseDown, ppb11.MouseDown, ppb12.MouseDown, ppb13.MouseDown, ppb14.MouseDown, ppb15.MouseDown, ppb16.MouseDown, ppb17.MouseDown, ppb18.MouseDown, ppb19.MouseDown, ppb20.MouseDown, ppb21.MouseDown, ppb22.MouseDown, ppb23.MouseDown, ppb24.MouseDown
        Dim myImage As Image = sender
        If exchange = True Then
            Dim MoveUp As New Animation.DoubleAnimation
            MoveUp.To = 400
            MoveUp.Duration = TimeSpan.FromSeconds(0.1)
            If myImage.Opacity = 1.0 And Xout < 6 Then
                myImage.Opacity = 0.98
                myImage.BeginAnimation(TopProperty, MoveUp)
                Xout += 1
                IndexOut(Xout - 1) = CInt(Strings.Mid(sender.Name, 4))
            Else
                myImage.Opacity = 1.0
                MoveUp.To = 420
                myImage.BeginAnimation(TopProperty, MoveUp)
                Xout -= 1
                If Xout > 0 Then IndexOut(Xout - 1) = CInt(Strings.Mid(myImage.Name, 4))
            End If
            If Xin = Xout Then
                btnExchange.Content = "Exchange(" + CStr(Xin) + ")"
                btnExchange.Visibility = Visibility.Visible
            Else
                btnExchange.Visibility = Visibility.Hidden
            End If
        Else  'play from your hand
            If played = False And ValidCard(CfromTag(myImage.Tag)) = True Then
                'btnContinue.Visibility = Visibility.Hidden
                PlayerNameLabel.Content = "You"
                Dim currcard As Card = CfromTag(myImage.Tag)
                LoadCard(ppby, currcard)  'show played card on table
                Dim sy As Animation.Storyboard
                sy = CType(Me.FindResource("PlayYou"), Animation.Storyboard)
                AddHandler sy.Completed, AddressOf sy_Completed
                trick.Add(currcard)   'add card to the trick
                Dim cardindex As Integer
                For i As Integer = 0 To _You.Hand.NumCards - 1
                    If _You.Hand.Cards(i).ToString = currcard.ToString Then cardindex = i
                Next
                _You.Hand.Cards.RemoveAt(cardindex) 'remove it from your hand
                myImage.ToolTip = ""
                myImage.Visibility = Visibility.Hidden
                ppby.Visibility = Visibility.Visible
                Me.BeginStoryboard(sy)
                'redisplay your hand
                Dim ic As Image
                Dim nL As Double = 409 - (((_You.Hand.NumCards - 1) * 35) / 2)
                For i As Integer = 24 To 1 Step -1
                    Dim Shift As New Animation.DoubleAnimation
                    Shift.Duration = TimeSpan.FromSeconds(0.25)
                    ic = FindName("ppb" + CStr(i))
                    If ic.Visibility = Visibility.Visible Then
                        Dim currL As Double = ic.GetValue(LeftProperty)
                        Shift.To = nL
                        ic.BeginAnimation(LeftProperty, Shift)
                        nL += 35
                    End If
                Next
                played = True
                If Leader = "You" Then
                    PlaySecond("East")
                    PlayThird("West")
                ElseIf Leader = "West" Then
                    PlayThird("East")
                End If
            End If
        End If
    End Sub
    Private Function ValidCard(ByVal vc As Card) As Boolean
        ValidCard = False
        If vc.FaceVal = FaceValue.Fool Then
            ValidCard = True
            Return ValidCard
        End If
        If trick.Count = 0 Then 'playing first
            ValidCard = True
            Return ValidCard
        End If
        If trick.Count = 1 Then 'playing second
            If vc.FaceVal = FaceValue.Fool Then
                ValidCard = True
                Return ValidCard
            End If
            If trick(0).FaceVal = FaceValue.Fool Then
                ValidCard = True
                Return ValidCard
            Else
                If vc.Suit = trick(0).Suit Then
                    If vc.PlayValue > trick(0).PlayValue Then
                        ValidCard = True
                        Return ValidCard
                    Else
                        Dim higher As Boolean = False
                        For i As Integer = 0 To _You.Hand.NumCards - 1
                            If _You.Hand.Cards(i).Suit = trick(0).Suit And _You.Hand.Cards(i).PlayValue > trick(0).PlayValue Then
                                higher = True
                            End If
                        Next
                        If higher = False Then
                            ValidCard = True
                            Return ValidCard
                        End If
                    End If
                End If
                If _You.Hand.ContainsSuit(trick(0).Suit) = False Then
                    If vc.Suit = Suit.Trumps Then
                        ValidCard = True
                        Return ValidCard
                    End If
                End If
                If _You.Hand.ContainsSuit(trick(0).Suit) = False And _You.Hand.ContainsSuit(Suit.Trumps) = False Then
                    ValidCard = True
                    Return ValidCard
                End If
            End If
        End If
        If trick.Count = 2 Then  'playing third
            If vc.FaceVal = FaceValue.Fool Then
                ValidCard = True
                Return ValidCard
            End If
            Dim leadsuit As Suit = trick(0).Suit
            If trick(0).FaceVal = FaceValue.Fool Then leadsuit = trick(1).Suit
            If _You.Hand.ContainsSuit(leadsuit) Then
                Dim hicard As Integer = 0
                If trick(0).FaceVal = FaceValue.Fool Then
                    hicard = trick(1).PlayValue
                Else
                    If trick(0).Suit = trick(1).Suit And trick(0).PlayValue > trick(1).PlayValue Then
                        hicard = trick(0).PlayValue
                    ElseIf trick(0).Suit = trick(1).Suit And trick(1).PlayValue > trick(0).PlayValue Then
                        hicard = trick(1).PlayValue
                    End If
                End If
                If vc.PlayValue > hicard And vc.Suit = leadsuit Then
                    ValidCard = True
                End If
                If leadsuit <> Suit.Trumps And trick(1).Suit = Suit.Trumps Then
                    If vc.Suit = leadsuit Then
                        ValidCard = True
                    End If
                End If
                Dim higher As Boolean = False
                For i As Integer = 0 To _You.Hand.NumCards - 1
                    If _You.Hand.Cards(i).Suit = leadsuit And _You.Hand.Cards(i).PlayValue > hicard Then
                        higher = True
                    End If
                Next
                If higher = False Then
                    ValidCard = True
                End If
                Return ValidCard
            Else ' no leadsuit
                If _You.Hand.ContainsSuit(Suit.Trumps) Then
                    If trick(1).Suit <> Suit.Trumps Then
                        If vc.Suit = Suit.Trumps Then
                            ValidCard = True
                            Return ValidCard
                        End If
                    Else
                        If vc.Suit = Suit.Trumps And vc.PlayValue > trick(1).PlayValue Then
                            ValidCard = True
                            Return ValidCard
                        End If
                        Dim higher As Boolean = False
                        For i As Integer = 0 To _You.Hand.NumCards - 1
                            If _You.Hand.Cards(i).Suit = Suit.Trumps And _You.Hand.Cards(i).PlayValue > trick(1).PlayValue Then
                                higher = True
                            End If
                        Next
                        If higher = False And vc.Suit = Suit.Trumps Then
                            ValidCard = True
                            Return ValidCard
                        End If
                        If _You.Hand.ContainsSuit(Suit.Trumps) = False And _You.Hand.ContainsSuit(leadsuit) = False Then
                            ValidCard = True
                            Return ValidCard
                        End If
                    End If
                Else ' no leadsuit and no trumps
                    ValidCard = True
                    Return ValidCard
                End If
            End If
        End If
    End Function

    Private Sub ExchangeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExchange.Click
        Dim MoveUp As New Animation.DoubleAnimation
        MoveUp.To = 420
        MoveUp.Duration = TimeSpan.FromSeconds(0.05)
        exchange = False
        Leader = "East"
        If My.Settings.UseSound = True Then
            splayer = New System.Media.SoundPlayer("click.wav")
            splayer.Play()
        End If
        Dim tc As Card
        For i As Integer = 0 To 5
            If IndexIn(i) <> 0 And IndexOut(i) <> 0 Then
                tc = _dog.Hand.Cards(IndexIn(i) - 1)
                _dog.Hand.Cards(IndexIn(i) - 1) = _You.Hand.Cards(IndexOut(i) - 1)
                _You.Hand.Cards(IndexOut(i) - 1) = tc
            End If
        Next
        'sort and show your hand and hide dog cards
        SortHand(_You.Hand)
        For i As Integer = 0 To 23
            If _playerCards(i).Opacity = 0.98 Then
                _playerCards(i).Opacity = 1.0
                _playerCards(i).BeginAnimation(TopProperty, MoveUp)
            End If
            LoadCard(_playerCards(i), _You.Hand.Cards(i))
            _playerCards(i).Visibility = Visibility.Visible
            _playerCards(i).ToolTip = _You.Hand.Cards(i).ToString
        Next
        For i As Integer = 1 To 6
            ImageVis("dpb", i, False)
        Next
        btnExchange.Visibility = Visibility.Hidden
        PlayerNameLabel.Content = "You"
        Label17.Visibility = Visibility.Visible
        Label18.Visibility = Visibility.Visible
        ShowMeld()
        PlayLoop()
    End Sub
    Private Sub ShowMeld()
        Dim pY As New Animation.Storyboard
        pY = CType(Me.FindResource("PYouShow"), Animation.Storyboard)
        Dim pE As New Animation.Storyboard
        pE = CType(Me.FindResource("PEastShow"), Animation.Storyboard)
        Dim pW As New Animation.Storyboard
        pW = CType(Me.FindResource("PWestShow"), Animation.Storyboard)
        _You.Hand.GetMeld(_You.Hand)
        LYLoTrump.Content = _You.Hand.LoTrump.ToString
        LYHiTrump.Content = _You.Hand.HiTrump.ToString
        LYSeq4.Content = _You.Hand.Seq4.ToString
        LYSeq7.Content = _You.Hand.Seq7.ToString
        LYSeq10.Content = _You.Hand.Seq10.ToString
        LYTarotTrump.Content = _You.Hand.TarotTrump.ToString
        LYMeld.Content = CStr(_You.Hand.LoTrump + _You.Hand.HiTrump + _You.Hand.TarotTrump + _You.Hand.Seq4 + _You.Hand.Seq7 + _You.Hand.Seq10)
        _West.Hand.GetMeld(_West.Hand)
        LWLoTrump.Content = _West.Hand.LoTrump.ToString
        LWHiTrump.Content = _West.Hand.HiTrump.ToString
        LWSeq4.Content = _West.Hand.Seq4.ToString
        LWSeq7.Content = _West.Hand.Seq7.ToString
        LWSeq10.Content = _West.Hand.Seq10.ToString
        LWTarotTrump.Content = _West.Hand.TarotTrump.ToString
        LWMeld.Content = CStr(_West.Hand.LoTrump + _West.Hand.HiTrump + _West.Hand.TarotTrump + _West.Hand.Seq4 + _West.Hand.Seq7 + _West.Hand.Seq10)
        _East.Hand.GetMeld(_East.Hand)
        LELoTrump.Content = _East.Hand.LoTrump.ToString
        LEHiTrump.Content = _East.Hand.HiTrump.ToString
        LESeq4.Content = _East.Hand.Seq4.ToString
        LESeq7.Content = _East.Hand.Seq7.ToString
        LESeq10.Content = _East.Hand.Seq10.ToString
        LETarotTrump.Content = _East.Hand.TarotTrump.ToString
        LEMeld.Content = CStr(_East.Hand.LoTrump + _East.Hand.HiTrump + _East.Hand.TarotTrump + _East.Hand.Seq4 + _East.Hand.Seq7 + _East.Hand.Seq10)
        If _You.IsDealer = True Then
            LYTotal.Content = CStr(YTrickPoints + CInt(LYMeld.Content))
            LETotal.Content = LEMeld.Content
            LWTotal.Content = LWMeld.Content
            If (YTrickPoints + CInt(LYMeld.Content)) > 0 Then
                PYou.Content = CStr(YTrickPoints + CInt(LYMeld.Content))
                PYou.Visibility = Visibility.Visible
                Me.BeginStoryboard(pY)
            End If
            If CInt(LEMeld.Content) > 0 Then
                PEast.Content = LEMeld.Content
                PEast.Visibility = Visibility.Visible
                Me.BeginStoryboard(pE)
            End If
            If CInt(LWMeld.Content) > 0 Then
                PWest.Content = LWMeld.Content
                PWest.Visibility = Visibility.Visible
                Me.BeginStoryboard(pW)
            End If
        ElseIf _East.IsDealer Then
            LETotal.Content = CStr(ETrickPoints + CInt(LEMeld.Content))
            LYTotal.Content = LYMeld.Content
            LWTotal.Content = LWMeld.Content
            If (ETrickPoints + CInt(LEMeld.Content)) > 0 Then
                PEast.Content = CStr(ETrickPoints + CInt(LEMeld.Content))
                PEast.Visibility = Visibility.Visible
                Me.BeginStoryboard(pE)
            End If
            If CInt(LYMeld.Content) > 0 Then
                PYou.Content = LYMeld.Content
                PYou.Visibility = Visibility.Visible
                Me.BeginStoryboard(pY)
            End If
            If CInt(LWMeld.Content) > 0 Then
                PWest.Content = LWMeld.Content
                PWest.Visibility = Visibility.Visible
                Me.BeginStoryboard(pW)
            End If
        ElseIf _West.IsDealer = True Then
            LWTotal.Content = CStr(WTrickPoints + CInt(LWMeld.Content))
            LETotal.Content = LEMeld.Content
            LYTotal.Content = LYMeld.Content
            If (WTrickPoints + CInt(LWMeld.Content)) > 0 Then
                PWest.Content = CStr(WTrickPoints + CInt(LWMeld.Content))
                PWest.Visibility = Visibility.Visible
                Me.BeginStoryboard(pW)
            End If
            If CInt(LEMeld.Content) > 0 Then
                PEast.Content = LEMeld.Content
                PEast.Visibility = Visibility.Visible
                Me.BeginStoryboard(pE)
            End If
            If CInt(LYMeld.Content) > 0 Then
                PYou.Content = LYMeld.Content
                PYou.Visibility = Visibility.Visible
                Me.BeginStoryboard(pY)
            End If
        End If
    End Sub
   
    Private Sub Points(ByVal Lead As String)
        Dim point As Integer = 1
        For i As Integer = 0 To 2
            If trick(i).Suit = Suit.Trumps Then
                Select Case trick(i).FaceVal
                    Case Is = FaceValue.TwentyOne
                        point += 4
                    Case Is = FaceValue.Ace
                        point += 4
                    Case Else
                        point += 1
                End Select
            End If
            If trick(i).FaceVal = FaceValue.King Then point += 4
            If trick(i).FaceVal = FaceValue.Queen Then point += 3
            If trick(i).FaceVal = FaceValue.Knight Then point += 2
            If trick(i).FaceVal = FaceValue.Jack Then point += 1
        Next
        'who won this trick?
        Dim who As String = ""
        Dim leadsuit As Suit
        If trick(0).FaceVal <> FaceValue.Fool Then
            leadsuit = trick(0).Suit
        Else
            leadsuit = trick(1).Suit
        End If
        Dim hival As Integer = 0
        Dim hitrump As Integer = 0
        Dim windex As Integer
        For i As Integer = 0 To 2
            If trick(i).Suit = leadsuit And trick(i).PlayValue > hival Then
                hival = trick(i).PlayValue
                windex = i
            End If
        Next
        For i As Integer = 0 To 2
            If leadsuit <> Suit.Trumps And trick(i).Suit = Suit.Trumps And trick(i).PlayValue > hitrump Then
                hitrump = trick(i).PlayValue
                windex = i
            End If
        Next
        Dim pY As New Animation.Storyboard
        pY = CType(Me.FindResource("PYouShow"), Animation.Storyboard)
        Dim pE As New Animation.Storyboard
        pE = CType(Me.FindResource("PEastShow"), Animation.Storyboard)
        Dim pW As New Animation.Storyboard
        pW = CType(Me.FindResource("PWestShow"), Animation.Storyboard)
        Select Case windex
            Case Is = 0
                who = Leader
            Case Is = 1
                If Lead = "You" Then who = "East"
                If Lead = "West" Then who = "You"
                If Lead = "East" Then who = "West"
            Case Is = 2
                If Lead = "You" Then who = "West"
                If Lead = "West" Then who = "East"
                If Lead = "East" Then who = "You"
        End Select
        Select Case who
            Case Is = "You"
                YTrickPoints += point
                PYou.Content = CStr(point)
                PYou.Visibility = Windows.Visibility.Visible
                Me.BeginStoryboard(pY)
                LYTotal.Content = CStr(YTrickPoints + CInt(LYMeld.Content))
            Case Is = "East"
                ETrickPoints += point
                PEast.Content = CStr(point)
                PEast.Visibility = Visibility.Visible
                Me.BeginStoryboard(pE)
                LETotal.Content = CStr(ETrickPoints + CInt(LEMeld.Content))
            Case Is = "West"
                WTrickPoints += point
                PWest.Content = CStr(point)
                PWest.Visibility = Visibility.Visible
                Me.BeginStoryboard(pW)
                LWTotal.Content = CStr(WTrickPoints + CInt(LWMeld.Content))
        End Select
        Dim y As Integer = CInt(LYTotal.Content)
        Dim e As Integer = CInt(LETotal.Content)
        Dim w As Integer = CInt(LWTotal.Content)
        Dim diff As Integer = 0
        Dim cLead As String = ""
        If y >= e And y >= w Then
            cLead = "You +"
            If e >= w Then
                diff = y - e
            Else
                diff = y - w
            End If
        End If
        If e > y And e > w Then
            cLead = "East +"
            If y > w Then
                diff = e - y
            Else
                diff = e - w
            End If
        End If
        If w > y And w > e Then
            cLead = "West +"
            If y > e Then
                diff = w - y
            Else
                diff = w - e
            End If
        End If
        TextStatus.Text = cLead + CStr(diff)
        'assign new Leader
        Leader = who
        'add trick to waste
        waste.Add(trick(0))
        waste.Add(trick(1))
        waste.Add(trick(2))
        trick.Clear()
    End Sub

    Private Sub Btn_Continue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click
        If played = False Then Exit Sub
        If My.Settings.UseSound = True Then
            splayer = New System.Media.SoundPlayer("click.wav")
            splayer.Play()
        End If
        If win = True Then
            win = False
            LYScore.Content = "0"
            LEScore.Content = "0"
            LWScore.Content = "0"
            TextStatus.Text = ""
            WinningDisplay()
        End If
        If LblScoring.Visibility = Visibility.Visible Then
            PlayerNameLabel.Content = "You"
            PlayNextHand()
            btnDeal.IsEnabled = True
            btnDeal.Visibility = Visibility.Visible
            btnContinue.Visibility = Visibility.Hidden
            LblScoring.Visibility = Visibility.Hidden
            Exit Sub
        End If
        ppby.Visibility = Visibility.Hidden
        ppbw.Visibility = Visibility.Hidden
        ppbe.SetCurrentValue(VisibilityProperty, Visibility.Hidden)
        btnContinue.Visibility = Visibility.Hidden
        If LblScoring.Visibility = Visibility.Hidden Then Points(Leader)
        Dim Yp As Integer = CInt(LYTotal.Content)
        Dim Wp As Integer = CInt(LWTotal.Content)
        Dim Ep As Integer = CInt(LETotal.Content)
        Dim hi As Integer = 0
        Dim sec As Integer = 0
        Dim wint As String = ""
        'check for last -- 24 tricks
        If waste.Count = 72 Then
            ClearTable()
            DealNewGame()
            If Yp >= Wp And Yp >= Ep Then
                wint = "You"
                hi = Yp
                If Wp >= Ep Then
                    sec = Wp
                Else
                    sec = Ep
                End If
            End If
            If Wp >= Yp And Wp >= Ep Then
                wint = "West"
                hi = Wp
                If Yp >= Ep Then
                    sec = Yp
                Else
                    sec = Ep
                End If
            End If
            If Ep >= Wp And Ep >= Yp Then
                wint = "East"
                hi = Ep
                If Yp >= Wp Then
                    sec = Yp
                Else
                    sec = Wp
                End If
            End If
            If wint = "You" Then
                LblScoring.Content = wint & " win (" & CStr(hi) & " - " & CStr(sec) & ") = " & CStr(hi - sec) & " points!"
            Else
                LblScoring.Content = wint & " wins (" & CStr(hi) & " - " & CStr(sec) & ") = " & CStr(hi - sec) & " points!"
            End If
            LblScoring.Visibility = Visibility.Visible
            If My.Settings.UseSound = True Then
                splayer = New System.Media.SoundPlayer("Bingwav.wav")
                splayer.Play()
            End If
            TextStatus.Text = ""
            Dim winame As String = ""
            Select Case wint
                Case Is = "East"
                    LEScore.Content = CStr((CInt(LEScore.Content) + hi - sec))
                    If CInt(LEScore.Content) >= CInt(My.Settings.Points2Win) Then
                        winame = "East is"
                    End If
                Case Is = "West"
                    LWScore.Content = CStr((CInt(LWScore.Content) + hi - sec))
                    If CInt(LWScore.Content) >= CInt(My.Settings.Points2Win) Then
                        winame = "West is"
                    End If
                Case Is = "You"
                    LYScore.Content = CStr((CInt(LYScore.Content) + hi - sec))
                    If CInt(LYScore.Content) >= CInt(My.Settings.Points2Win) Then
                        winame = "You are"
                    End If
            End Select
            _newHand = True
            'check for overall win
            If winame <> "" Then
                If My.Settings.UseSound = True Then
                    splayer = New System.Media.SoundPlayer("sparclewav.wav")
                    splayer.Play()
                End If
                LblScoring.Content = "Game Over! " & winame & " the winner."
                LblScoring.Visibility = Visibility.Visible
                win = True
            End If
            btnContinue.Visibility = Visibility.Visible
        Else
            PlayLoop()
        End If
    End Sub
    Private Sub PlayLoop()
        played = False
        Select Case Leader
            Case Is = "You"
                Label18.Content = "West"
                Label17.Content = "East"
                PlayerNameLabel.Content = "You - Click a card to play"
            Case Is = "West"
                PlayerNameLabel.Content = "You"
                Label18.Content = "West - Leading"
                Label17.Content = "East"
                PlayFirst("West")
                PlayerNameLabel.Content = "You - Click a card to play"
            Case Is = "East"
                Label17.Content = "East - Leading"
                Label18.Content = "West"
                PlayerNameLabel.Content = "You"
                PlayFirst("East")
                PlaySecond("West")
                PlayerNameLabel.Content = "You - Click a card to play"
        End Select
    End Sub
    Private Sub PlayFirst(ByVal who As String)
        Dim cindex As Integer = -1
        Dim _P As New Player
        Dim longsuit As Suit
        Dim shortsuit As Suit
        If who = "West" Then _P = _West
        If who = "East" Then _P = _East
        'find long suit
        Dim cClubs As Integer = _P.Hand.CountSuit(Suit.Clubs)
        Dim cSpades As Integer = _P.Hand.CountSuit(Suit.Spades)
        Dim cHearts As Integer = _P.Hand.CountSuit(Suit.Hearts)
        Dim cDiamonds As Integer = _P.Hand.CountSuit(Suit.Diamonds)
        Dim cTrumps As Integer = _P.Hand.CountSuit(Suit.Trumps)
        If cClubs >= cSpades And cClubs >= cHearts And cClubs >= cDiamonds Then longsuit = Suit.Clubs
        If cSpades >= cClubs And cSpades >= cHearts And cSpades >= cDiamonds Then longsuit = Suit.Spades
        If cHearts >= cClubs And cHearts >= cSpades And cHearts >= cDiamonds Then longsuit = Suit.Hearts
        If cDiamonds >= cClubs And cDiamonds >= cSpades And cDiamonds >= cHearts Then longsuit = Suit.Diamonds
        'find short suit
        If cClubs <= cSpades And cClubs <= cHearts And cClubs <= cDiamonds And cClubs <> 0 Then shortsuit = Suit.Clubs
        If cSpades <= cClubs And cSpades <= cHearts And cSpades <= cDiamonds And cSpades <> 0 Then shortsuit = Suit.Spades
        If cHearts <= cClubs And cHearts <= cSpades And cHearts <= cDiamonds And cHearts <> 0 Then shortsuit = Suit.Hearts
        If cDiamonds <= cClubs And cDiamonds <= cSpades And cDiamonds <= cHearts And cDiamonds <> 0 Then shortsuit = Suit.Diamonds
        'find hicard in short suit
        Dim hicard As Integer = 0
        Dim hivalue As FaceValue
        Dim tempindex As Integer = 0
        If cClubs <> 0 And cHearts <> 0 And cSpades <> 0 And cDiamonds <> 0 Then
            For i As Integer = 0 To _P.Hand.NumCards - 1
                If _P.Hand.Cards(i).Suit = shortsuit And _P.Hand.Cards(i).PlayValue > hicard Then
                    hicard = _P.Hand.Cards(i).PlayValue
                    hivalue = _P.Hand.Cards(i).FaceVal
                    tempindex = i
                End If
            Next
            'check if it is a winner
            Dim winner As Boolean = True
            If waste.Count = 0 Then
                If hivalue <> FaceValue.King Then winner = False
            Else
                If hivalue <> FaceValue.King Then
                    Dim counth As Integer = 0
                    For j As Integer = hicard + 1 To 14
                        For i As Integer = 0 To waste.Count - 1
                            If waste(i).Suit = shortsuit And waste(i).PlayValue = j Then counth += 1
                        Next
                    Next
                    If counth < (14 - hicard) Then winner = False
                End If
            End If
            If winner = True Then cindex = tempindex
            Dim wtcount As Integer = 0
            hivalue = 0
            If cindex = -1 Then 'check to see if trump can be run out
                For i As Integer = 0 To waste.Count - 1
                    If waste(i).Suit = Suit.Trumps Then wtcount += 1
                Next
                If cTrumps <> 0 And cTrumps > (21 - wtcount - cTrumps) Then
                    'Can run out trumps so lead trump
                    For i As Integer = 0 To _P.Hand.NumCards - 1
                        If _P.Hand.Cards(i).PlayValue > hivalue Then
                            hivalue = _P.Hand.Cards(i).PlayValue
                            tempindex = i
                        End If
                    Next
                    cindex = tempindex
                End If
            End If
            If cindex = -1 Then ' play high card in longsuit
                hivalue = 0
                For i As Integer = 0 To _P.Hand.NumCards - 1
                    If _P.Hand.Cards(i).PlayValue > hivalue And _P.Hand.Cards(i).Suit = longsuit Then
                        tempindex = i
                        hivalue = _P.Hand.Cards(i).PlayValue
                    End If
                Next
                cindex = tempindex
            End If
        Else 'only trump left
            cindex = _P.Hand.NumCards - 1
        End If
        _P.Hand.Cards(cindex).IsCardUp = True
        If who = "West" Then
            LoadCard(ppbw, _P.Hand.Cards(cindex))  'show played card on table
            Dim s As Animation.Storyboard
            s = CType(Me.FindResource("PlayWest"), Animation.Storyboard)
            Me.BeginStoryboard(s)
            ppbw.Visibility = Visibility.Visible
        Else
            LoadCard(ppbe, _P.Hand.Cards(cindex))    
        End If
        trick.Add(_P.Hand.Cards(cindex))   'add card to the trick
        _P.Hand.Cards.RemoveAt(cindex) 'remove it from hand
        If who = "West" Then PlayerNameLabel.Content = "You - Click a card to play"
    End Sub
    Private Sub PlaySecond(ByVal who As String)
        Dim csuit As Suit
        Dim playcard As FaceValue
        Dim cindex As Integer
        Dim foundit As Boolean = False
        Dim _P As New Player
        If who = "West" Then _P = _West
        If who = "East" Then _P = _East
        'fool was led - play highest suit card
        If trick(0).FaceVal = FaceValue.Fool Then
            Dim highcard As Integer = 0
            For Each c As Card In _P.Hand.Cards
                If c.PlayValue > highcard And c.Suit <> Suit.Trumps Then
                    highcard = c.FaceVal
                    csuit = c.Suit
                End If
            Next
            playcard = highcard
        Else
            'beat card in same suit
            For Each c As Card In _P.Hand.Cards
                If c.PlayValue > trick(0).PlayValue And c.Suit = trick(0).Suit And foundit = False Then
                    csuit = trick(0).Suit
                    playcard = c.FaceVal
                    foundit = True
                End If
            Next
            'follow suit with low card
            If foundit = False Then
                For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                    If _P.Hand.Cards(i).Suit = trick(0).Suit And foundit = False Then
                        foundit = True
                        playcard = _P.Hand.Cards(i).FaceVal
                        csuit = trick(0).Suit
                    End If
                Next
            End If
            If foundit = False And trick(0).Suit <> Suit.Trumps Then 'play low trump
                If _P.Hand.ContainsSuit(Suit.Trumps) Then
                    Dim mintrump As FaceValue = 10000
                    For Each c As Card In _P.Hand.Cards
                        If c.Suit = Suit.Trumps And c.FaceVal < mintrump And c.FaceVal <> FaceValue.Fool Then
                            mintrump = c.FaceVal
                            playcard = c.FaceVal
                            csuit = Suit.Trumps
                            foundit = True
                        End If
                    Next
                Else 'no trumps play any low card
                    Dim mincard As Integer = 25
                    For Each c As Card In _P.Hand.Cards
                        If c.PlayValue < mincard Then
                            mincard = c.PlayValue
                            csuit = c.Suit
                        End If
                    Next
                    playcard = mincard
                    foundit = True
                End If
                If foundit = False And trick(0).Suit = Suit.Trumps And trick(0).FaceVal <> FaceValue.Fool Then
                    'trump was led - beat it
                    For i As Integer = 0 To _P.Hand.NumCards - 1
                        If _P.Hand.Cards(i).Suit = Suit.Trumps And _P.Hand.Cards(i).PlayValue > trick(0).PlayValue And foundit = False Then
                            foundit = True
                            csuit = Suit.Trumps
                            playcard = _P.Hand.Cards(i).FaceVal
                        End If
                    Next
                End If
                If foundit = False And trick(0).Suit = Suit.Trumps And trick(0).FaceVal <> FaceValue.Fool Then
                    'trump was led follow suit
                    If _P.Hand.ContainsSuit(Suit.Trumps) Then
                        For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                            If _P.Hand.Cards(i).Suit = Suit.Trumps And foundit = False Then
                                foundit = True
                                csuit = Suit.Trumps
                                playcard = _P.Hand.Cards(i).FaceVal
                            End If
                        Next
                    End If
                    'play lowest card
                    If foundit = False Then
                        Dim mincard As Integer = 25
                        For Each c As Card In _P.Hand.Cards
                            If c.PlayValue < mincard Then
                                mincard = c.PlayValue
                                csuit = c.Suit
                            End If
                        Next
                        playcard = mincard
                    End If
                End If
            End If
        End If
        For i As Integer = 0 To _P.Hand.NumCards - 1
            If _P.Hand.Cards(i).FaceVal = playcard And _P.Hand.Cards(i).Suit = csuit Then
                cindex = i
            End If
        Next
        _P.Hand.Cards(cindex).IsCardUp = True
        If who = "West" Then
            LoadCard(ppbw, _P.Hand.Cards(cindex))  'show played card on table
            Dim sb As Animation.Storyboard
            sb = CType(Me.FindResource("PlayEastThenWest"), Animation.Storyboard)
            Me.BeginStoryboard(sb)
            ppbe.Visibility = Visibility.Visible
            ppbw.Visibility = Visibility.Visible
        Else
            LoadCard(ppbe, _P.Hand.Cards(cindex))
            ppbe.Visibility = Visibility.Visible
        End If
        trick.Add(_P.Hand.Cards(cindex))   'add card to the trick
        _P.Hand.Cards.RemoveAt(cindex) 'remove it from hand
    End Sub
    Private Sub PlayThird(ByVal who As String)
        Dim cindex As Integer
        Dim foundit As Boolean = False
        Dim _P As New Player
        If who = "East" Then _P = _East
        If who = "West" Then _P = _West
        'check for leadsuit
        Dim leadsuit As Suit
        foundit = False
        If trick(0).FaceVal <> FaceValue.Fool Then
            leadsuit = trick(0).Suit
        Else
            leadsuit = trick(1).Suit
        End If
        'find high card value in leadsuit
        Dim hivalue As Integer = 0
        If trick(0).Suit = leadsuit And trick(0).PlayValue > hivalue Then
            hivalue = trick(0).PlayValue
        End If
        If trick(1).Suit = leadsuit And trick(1).PlayValue > hivalue Then
            hivalue = trick(1).PlayValue
        End If
        'not trumped play higher in leadsuit
        If trick(0).Suit <> Suit.Trumps And trick(1).Suit <> Suit.Trumps Then
            For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                If _P.Hand.Cards(i).Suit = leadsuit And _P.Hand.Cards(i).PlayValue > hivalue And foundit = False Then
                    foundit = True
                    cindex = i
                End If
            Next
            If foundit = False Then 'play lower in same suit
                For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                    If _P.Hand.Cards(i).Suit = leadsuit And foundit = False Then
                        foundit = True
                        cindex = i
                    End If
                Next
            End If
            If foundit = False And _P.Hand.ContainsSuit(Suit.Trumps) = True Then 'no suit - play trump
                For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                    If _P.Hand.Cards(i).Suit = Suit.Trumps And _P.Hand.Cards(i).FaceVal <> FaceValue.Fool And foundit = False Then
                        cindex = i
                        foundit = True
                    End If
                Next
            ElseIf foundit = False Then 'no trump - play any low card
                Dim lovalue As Integer = 1000
                For i As Integer = 0 To _P.Hand.NumCards - 1
                    If _P.Hand.Cards(i).PlayValue < lovalue Then
                        lovalue = _P.Hand.Cards(i).PlayValue
                        cindex = i
                    End If
                Next
            End If
        Else 'trump was led or played
            If trick(0).Suit = Suit.Trumps And trick(0).FaceVal <> FaceValue.Fool Then  'trump was led
                If _P.Hand.ContainsSuit(Suit.Trumps) = True Then
                    For i As Integer = 0 To _P.Hand.NumCards - 1
                        If _P.Hand.Cards(i).PlayValue > hivalue And _P.Hand.Cards(i).Suit = Suit.Trumps And foundit = False Then
                            cindex = i
                            foundit = True
                        End If
                    Next
                    If foundit = False Then ' play lower trump
                        For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                            If _P.Hand.Cards(i).Suit = Suit.Trumps And foundit = False Then
                                cindex = i
                                foundit = True
                            End If
                        Next
                    End If
                Else ' no trumps - play any low card
                    Dim lovalue As Integer = 1000
                    For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                        If _P.Hand.Cards(i).PlayValue < lovalue And foundit = False Then
                            lovalue = _P.Hand.Cards(i).PlayValue
                            cindex = i
                            foundit = True
                        End If
                    Next
                End If
            Else 'suit was trumped by second player
                If _P.Hand.ContainsSuit(leadsuit) = True Then
                    'play lower in lead suit
                    For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                        If _P.Hand.Cards(i).Suit = leadsuit And foundit = False Then
                            foundit = True
                            cindex = i
                        End If
                    Next
                Else ' play higher trump than trick(1)
                    For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                        If _P.Hand.Cards(i).PlayValue > trick(1).PlayValue And _P.Hand.Cards(i).Suit = Suit.Trumps And foundit = False Then
                            cindex = i
                            foundit = True
                        End If
                    Next
                    If foundit = False Then 'play lower trump
                        For i As Integer = _P.Hand.NumCards - 1 To 0 Step -1
                            If _P.Hand.Cards(i).Suit = Suit.Trumps And foundit = False Then
                                cindex = i
                                foundit = True
                            End If
                        Next
                    End If
                    If foundit = False Then 'play any low card
                        Dim lovalue As Integer = 1000
                        For i As Integer = 0 To _P.Hand.NumCards - 1
                            If _P.Hand.Cards(i).PlayValue < lovalue Then
                                lovalue = _P.Hand.Cards(i).PlayValue
                                cindex = i
                            End If
                        Next
                    End If
                End If
            End If
        End If
        _P.Hand.Cards(cindex).IsCardUp = True
        If who = "East" Then
            LoadCard(ppbe, _P.Hand.Cards(cindex))  'show played card on table
            If Leader <> "You" Then
                Dim s5 As Animation.Storyboard
                s5 = CType(Me.FindResource("PlayEast"), Animation.Storyboard)
                AddHandler s5.Completed, AddressOf sc_Completed
                Me.BeginStoryboard(s5)
                ppbe.Visibility = Visibility.Visible
            End If
        Else
            LoadCard(ppbw, _P.Hand.Cards(cindex))  'show played card on table
            Dim sb As Animation.Storyboard
            sb = CType(Me.FindResource("PlayEastThenWest"), Animation.Storyboard)
            AddHandler sb.Completed, AddressOf sc_Completed
            Me.BeginStoryboard(sb)
            ppbw.Visibility = Visibility.Visible
            ppbe.Visibility = Visibility.Visible
        End If
        trick.Add(_P.Hand.Cards(cindex))   'add card to the trick
        _P.Hand.Cards.RemoveAt(cindex) 'remove it from hand
    End Sub
    Private Sub sc_Completed(ByVal sender As Object, ByVal e As EventArgs)
        btnContinue.Visibility = Visibility.Visible
    End Sub
    Private Sub ChooseFromKitty(ByVal wp As String)
        Dim _P As New Player
        Dim C_out As New List(Of Card)
        If wp = "West" Then _P = _West
        If wp = "East" Then _P = _East
        'take trump and court cards
        For i As Integer = 0 To 5
            If _dog.Hand.Cards(i).PlayValue > 10 Or _dog.Hand.Cards(i).PlayValue = 0 Then
                C_out.Add(_dog.Hand.Cards(i))
            End If
        Next
        If C_out.Count > 0 Then
            Dim tcard As Card
            For Each c As Card In C_out  ' exchange with the lowest playvalue card in hand
                Dim lowest As Integer = 10000
                Dim findex As Integer
                For i As Integer = 0 To 23
                    If _P.Hand.Cards(i).PlayValue < lowest And _P.Hand.Cards(i).Suit <> Suit.Fool Then
                        lowest = _P.Hand.Cards(i).PlayValue
                        findex = i
                    End If
                Next
                tcard = _P.Hand.Cards(findex)
                _P.Hand.Cards.RemoveAt(findex)
                _P.Hand.Cards.Add(c)
                _dog.Hand.Cards.Remove(c)
                _dog.Hand.Cards.Add(tcard)
            Next
            _P.Hand.SortHand(_P.Hand)
        End If
        C_out.Clear()
    End Sub

    Private Sub Image1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Image1.MouseDown
        If btnContinue.Visibility = Visibility.Visible Then
            Btn_Continue_Click(sender, e)
        End If
    End Sub

    Private Sub BtnOptions_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles BtnOptions.Click
        My.Windows.OptionsWindow.Show()
    End Sub

    Private Sub WinningDisplay()
        Dim r As Integer
        Randomize()
        r = CInt(Math.Floor((22) * Rnd()))
        Dim tmean(21) As String
        tmean(0) = "With all his worldly possessions in one small pack, the Fool travels he knows not where. So filled with visions and daydreams is he, that he doesn't see the cliff he is likely to fall over. At his heel, a small dog harries him (or tries to warn him of a possible mis-step). "
        tmean(1) = "Skillful, self-confident, a powerful magus with the Infinite as a halo floating above his head, the Magician reveals all possibilities - the Sword, Cup, Wand and Pentacle are laid out before him."
        tmean(2) = "A beautiful and mysterious lady enthroned between two pillars and illuminated by the moon, the High Priestess has secret knowledge to impart."
        tmean(3) = "The golden-haired Empress walks through a garden lush with grain and an abundance of fruit.  She is the giver of Earthly gifts."
        tmean(4) = "The Emperor atop his stone throne rules the empire he created.  His leadership is as solid as the mountains that surround him."
        tmean(5) = "A wise and holy man, the Heirophant passes on his teachings to those acolytes that kneel before him."
        tmean(6) = "Adam and Eve with fruit in hand stand under the trees with Cupid hovering.  Their choices, made in love, set them onto their path to their future."
        tmean(7) = "The charioteer succeeds in battle by attacking from the side.  Controlling the horses, who appear to be trying to pull in opposite directions, will require confidence and a firm hand to the reins."
        tmean(8) = "The maiden's pure and innocent strength prove more than enough to tame the fierce lion."
        tmean(9) = "Carrying his staff and lantern the Hermit travels a barren landscape.  Always moving, searching, but with time to contemplate his journey."
        tmean(10) = "Mythical beasts (Lion, Bull, Cherub and Eagle) represent the four corners of the universe as the Wheel of Fortune turns in the center.  Cherubs atop the wheel are disinterested in the eventual outcome of a spin of Chance."
        tmean(11) = "Justice sits her throne and looks on with impartial interest as she wields a sharp sword in her right hand and the scales in her left."
        tmean(12) = "The Hanged Man appears contemplative in his uncormfortable position.  But clearly his vulnerablity has led to illumination as evidenced by the halo around his head."
        tmean(13) = "Death in his armor atop his black charger strides across a devastated landscape.  All before him, of whatever age, must face him."
        tmean(14) = "Standing on the waves of an ocean which runs to towering mountains in the background, the angel is mixing the contents of her two cups, perhaps to dilute or somehow blend their properties."
        tmean(15) = "The goat-horned Devil looks down over his acolytes indulging in every earthly pleasure."
        tmean(16) = "The residents of the Tower in their self-importance and delusion are thrown to their deaths below by the brilliant bolt of Truth's lightening."
        tmean(17) = "On a clear starry night a beautiful girl fills a pool of water from her urn.  Stop to drink.  Refresh yourself and look to that brightest Star which seems to stand as a guide to a hopeful path ahead."
        tmean(18) = "The Moon reigns over a night-shrouded land where dogs howl at the moon and potential danger emerges from the deeps."
        tmean(19) = "The golden Sun beams warmth and energy down onto the frolicing children and flowers.  All below will have their 'day in the Sun'."
        tmean(20) = "Six fiery angels blow their trumpets from on high - and the dead rise from their graves on this Judgement Day, innocent and fully healed."
        tmean(21) = "The Lion, Bull, Cherub and Eagle representing the four corners of the universe are encompassed by the view of the dancer at the center of the World.  She sees all and finds favor in it."
        TextBlockWin.Text = tmean(r)
        Dim picn As String = "/Images/BigTrumps/tr" + CStr(r) + ".jpg"
        Dim bmw As New BitmapImage
        bmw.BeginInit()
        bmw.UriSource = New Uri(picn, UriKind.Relative)
        bmw.EndInit()
        ImageWin.Source = bmw
        ImageWin.Visibility = Visibility.Visible
        TextBlockWin.Visibility = Visibility.Visible
        Dim sbt As Animation.Storyboard
        sbt = CType(Me.FindResource("ShowBigTrump"), Animation.Storyboard)
        BeginStoryboard(sbt)
    End Sub

    Private Sub sy_Completed(ByVal sender As Object, ByVal e As EventArgs)
        If Leader = "East" Then
            btnContinue.Visibility = Visibility.Visible
        Else
            btnContinue.Visibility = Visibility.Hidden
        End If
    End Sub

    Private Sub btnContinue_IsVisibleChanged(ByVal sender As System.Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles btnContinue.IsVisibleChanged
        If played = False Then
            btnContinue.Visibility = Visibility.Hidden
        Else
            Exit Sub
        End If
    End Sub
End Class
